.class public Lcom/zuiapps/suite/utils/security/RSATool;
.super Ljava/lang/Object;
.source "RSATool.java"


# direct methods
.method public constructor <init>()V
    .registers 1

    .prologue
    .line 26
    invoke-direct {p0}, Ljava/lang/Object;-><init>()V

    .line 27
    return-void
.end method

.method public static creatmyKey()Ljava/security/KeyPair;
    .registers 8

    .prologue
    .line 108
    invoke-static {}, Ljava/lang/System;->currentTimeMillis()J

    move-result-wide v4

    .line 110
    .local v4, "mySeed":J
    :try_start_4
    const-string v6, "RSA"

    invoke-static {v6}, Ljava/security/KeyPairGenerator;->getInstance(Ljava/lang/String;)Ljava/security/KeyPairGenerator;

    move-result-object v1

    .line 111
    .local v1, "keyGen":Ljava/security/KeyPairGenerator;
    const-string v6, "SHA1PRNG"

    const-string v7, "SUN"

    invoke-static {v6, v7}, Ljava/security/SecureRandom;->getInstance(Ljava/lang/String;Ljava/lang/String;)Ljava/security/SecureRandom;

    move-result-object v3

    .line 112
    .local v3, "random":Ljava/security/SecureRandom;
    invoke-virtual {v3, v4, v5}, Ljava/security/SecureRandom;->setSeed(J)V

    .line 113
    const/16 v6, 0x400

    invoke-virtual {v1, v6, v3}, Ljava/security/KeyPairGenerator;->initialize(ILjava/security/SecureRandom;)V

    .line 114
    invoke-virtual {v1}, Ljava/security/KeyPairGenerator;->generateKeyPair()Ljava/security/KeyPair;
    :try_end_1d
    .catch Ljava/lang/Exception; {:try_start_4 .. :try_end_1d} :catch_1f

    move-result-object v2

    .line 118
    .end local v1    # "keyGen":Ljava/security/KeyPairGenerator;
    .end local v3    # "random":Ljava/security/SecureRandom;
    :goto_1e
    return-object v2

    .line 115
    :catch_1f
    move-exception v0

    .line 116
    .local v0, "e1":Ljava/lang/Exception;
    const/4 v2, 0x0

    goto :goto_1e
.end method

.method public static decryptByRSA([B[B)[B
    .registers 8
    .param p0, "privKeyInByte"    # [B
    .param p1, "data"    # [B

    .prologue
    .line 154
    :try_start_0
    new-instance v4, Ljava/security/spec/PKCS8EncodedKeySpec;

    invoke-direct {v4, p0}, Ljava/security/spec/PKCS8EncodedKeySpec;-><init>([B)V

    .line 155
    .local v4, "priv_spec":Ljava/security/spec/PKCS8EncodedKeySpec;
    const-string v5, "RSA"

    invoke-static {v5}, Ljava/security/KeyFactory;->getInstance(Ljava/lang/String;)Ljava/security/KeyFactory;

    move-result-object v2

    .line 156
    .local v2, "mykeyFactory":Ljava/security/KeyFactory;
    invoke-virtual {v2, v4}, Ljava/security/KeyFactory;->generatePrivate(Ljava/security/spec/KeySpec;)Ljava/security/PrivateKey;

    move-result-object v3

    .line 157
    .local v3, "privKey":Ljava/security/PrivateKey;
    const-string v5, "RSA/ECB/PKCS1Padding"

    invoke-static {v5}, Ljavax/crypto/Cipher;->getInstance(Ljava/lang/String;)Ljavax/crypto/Cipher;

    move-result-object v0

    .line 158
    .local v0, "cipher":Ljavax/crypto/Cipher;
    const/4 v5, 0x2

    invoke-virtual {v0, v5, v3}, Ljavax/crypto/Cipher;->init(ILjava/security/Key;)V

    .line 159
    invoke-virtual {v0, p1}, Ljavax/crypto/Cipher;->doFinal([B)[B
    :try_end_1c
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_1c} :catch_1e

    move-result-object v5

    .line 161
    .end local v0    # "cipher":Ljavax/crypto/Cipher;
    .end local v2    # "mykeyFactory":Ljava/security/KeyFactory;
    .end local v3    # "privKey":Ljava/security/PrivateKey;
    .end local v4    # "priv_spec":Ljava/security/spec/PKCS8EncodedKeySpec;
    :goto_1d
    return-object v5

    .line 160
    :catch_1e
    move-exception v1

    .line 161
    .local v1, "e":Ljava/lang/Exception;
    const/4 v5, 0x0

    goto :goto_1d
.end method

.method public static encryptByRSA([B[B)[B
    .registers 8
    .param p0, "pubKeyInByte"    # [B
    .param p1, "data"    # [B

    .prologue
    .line 132
    :try_start_0
    const-string v5, "RSA"

    invoke-static {v5}, Ljava/security/KeyFactory;->getInstance(Ljava/lang/String;)Ljava/security/KeyFactory;

    move-result-object v2

    .line 133
    .local v2, "mykeyFactory":Ljava/security/KeyFactory;
    new-instance v4, Ljava/security/spec/X509EncodedKeySpec;

    invoke-direct {v4, p0}, Ljava/security/spec/X509EncodedKeySpec;-><init>([B)V

    .line 134
    .local v4, "pub_spec":Ljava/security/spec/X509EncodedKeySpec;
    invoke-virtual {v2, v4}, Ljava/security/KeyFactory;->generatePublic(Ljava/security/spec/KeySpec;)Ljava/security/PublicKey;

    move-result-object v3

    .line 135
    .local v3, "pubKey":Ljava/security/PublicKey;
    const-string v5, "RSA/ECB/PKCS1Padding"

    invoke-static {v5}, Ljavax/crypto/Cipher;->getInstance(Ljava/lang/String;)Ljavax/crypto/Cipher;

    move-result-object v0

    .line 136
    .local v0, "cipher":Ljavax/crypto/Cipher;
    const/4 v5, 0x1

    invoke-virtual {v0, v5, v3}, Ljavax/crypto/Cipher;->init(ILjava/security/Key;)V

    .line 137
    invoke-virtual {v0, p1}, Ljavax/crypto/Cipher;->doFinal([B)[B
    :try_end_1c
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_1c} :catch_1e

    move-result-object v5

    .line 139
    .end local v0    # "cipher":Ljavax/crypto/Cipher;
    .end local v2    # "mykeyFactory":Ljava/security/KeyFactory;
    .end local v3    # "pubKey":Ljava/security/PublicKey;
    .end local v4    # "pub_spec":Ljava/security/spec/X509EncodedKeySpec;
    :goto_1d
    return-object v5

    .line 138
    :catch_1e
    move-exception v1

    .line 139
    .local v1, "e":Ljava/lang/Exception;
    const/4 v5, 0x0

    goto :goto_1d
.end method

.method public static giveRSAKeyPairInByte()[Ljava/lang/Object;
    .registers 7

    .prologue
    .line 85
    invoke-static {}, Lcom/zuiapps/suite/utils/security/RSATool;->creatmyKey()Ljava/security/KeyPair;

    move-result-object v2

    .line 86
    .local v2, "newKeyPair":Ljava/security/KeyPair;
    if-nez v2, :cond_8

    .line 87
    const/4 v5, 0x0

    .line 96
    :goto_7
    return-object v5

    .line 89
    :cond_8
    const/4 v6, 0x2

    new-array v5, v6, [Ljava/lang/Object;

    .line 90
    .local v5, "re":[Ljava/lang/Object;
    invoke-virtual {v2}, Ljava/security/KeyPair;->getPrivate()Ljava/security/PrivateKey;

    move-result-object v3

    .line 91
    .local v3, "priv":Ljava/security/PrivateKey;
    invoke-interface {v3}, Ljava/security/PrivateKey;->getEncoded()[B

    move-result-object v0

    .line 92
    .local v0, "b_priv":[B
    invoke-virtual {v2}, Ljava/security/KeyPair;->getPublic()Ljava/security/PublicKey;

    move-result-object v4

    .line 93
    .local v4, "pub":Ljava/security/PublicKey;
    invoke-interface {v4}, Ljava/security/PublicKey;->getEncoded()[B

    move-result-object v1

    .line 94
    .local v1, "b_pub":[B
    const/4 v6, 0x0

    aput-object v0, v5, v6

    .line 95
    const/4 v6, 0x1

    aput-object v1, v5, v6

    goto :goto_7
.end method

.method public static main([Ljava/lang/String;)V
    .registers 12
    .param p0, "args"    # [Ljava/lang/String;

    .prologue
    .line 173
    :try_start_0
    invoke-static {}, Lcom/zuiapps/suite/utils/security/RSATool;->giveRSAKeyPairInByte()[Ljava/lang/Object;

    move-result-object v7

    .line 175
    .local v7, "v":[Ljava/lang/Object;
    const-string v9, "\u5047\u8bbe\u8fd9\u662f\u8981\u52a0\u5bc6\u7684\u5ba2\u6237\u6570\u636e"

    invoke-static {v9}, Lcom/zuiapps/suite/utils/security/Digest;->MdigestSHA(Ljava/lang/String;)[B

    move-result-object v5

    .line 177
    .local v5, "source":[B
    const/4 v9, 0x0

    aget-object v9, v7, v9

    check-cast v9, [B

    check-cast v9, [B

    invoke-static {v9, v5}, Lcom/zuiapps/suite/utils/security/RSATool;->sign([B[B)[B

    move-result-object v3

    .line 179
    .local v3, "sign":[B
    const/4 v9, 0x1

    aget-object v9, v7, v9

    check-cast v9, [B

    check-cast v9, [B

    invoke-static {v9, v5, v3}, Lcom/zuiapps/suite/utils/security/RSATool;->verify([B[B[B)Z

    move-result v8

    .line 180
    .local v8, "yes":Z
    if-eqz v8, :cond_29

    .line 181
    sget-object v9, Ljava/lang/System;->out:Ljava/io/PrintStream;

    const-string v10, "\u5339\u914d\u6210\u529f \u5408\u6cd5\u7684\u7b7e\u540d!"

    invoke-virtual {v9, v10}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V

    .line 185
    :cond_29
    invoke-static {}, Lcom/zuiapps/suite/utils/security/RSATool;->creatmyKey()Ljava/security/KeyPair;

    move-result-object v1

    .line 187
    .local v1, "kp":Ljava/security/KeyPair;
    const-string v9, "\u5047\u8bbe\u8fd9\u662f\u8981\u52a0\u5bc6\u7684\u5ba2\u6237\u6570\u636e"

    invoke-static {v9}, Lcom/zuiapps/suite/utils/security/Digest;->MdigestSHA(Ljava/lang/String;)[B

    move-result-object v6

    .line 190
    .local v6, "source1":[B
    invoke-virtual {v1}, Ljava/security/KeyPair;->getPublic()Ljava/security/PublicKey;

    move-result-object v9

    invoke-interface {v9}, Ljava/security/PublicKey;->getEncoded()[B

    move-result-object v9

    invoke-static {v9, v6}, Lcom/zuiapps/suite/utils/security/RSATool;->encryptByRSA([B[B)[B

    move-result-object v4

    .line 192
    .local v4, "sign1":[B
    invoke-virtual {v1}, Ljava/security/KeyPair;->getPrivate()Ljava/security/PrivateKey;

    move-result-object v9

    invoke-interface {v9}, Ljava/security/PrivateKey;->getEncoded()[B

    move-result-object v9

    invoke-static {v9, v4}, Lcom/zuiapps/suite/utils/security/RSATool;->decryptByRSA([B[B)[B

    move-result-object v2

    .line 194
    .local v2, "newSource1":[B
    invoke-static {v6, v2}, Ljava/util/Arrays;->equals([B[B)Z

    move-result v9

    if-eqz v9, :cond_58

    .line 195
    sget-object v9, Ljava/lang/System;->out:Ljava/io/PrintStream;

    const-string v10, "\u5339\u914d\u6210\u529f \u5408\u6cd5\u7684\u79c1\u94a5!"

    invoke-virtual {v9, v10}, Ljava/io/PrintStream;->print(Ljava/lang/String;)V
    :try_end_58
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_58} :catch_59

    .line 201
    .end local v1    # "kp":Ljava/security/KeyPair;
    .end local v2    # "newSource1":[B
    .end local v3    # "sign":[B
    .end local v4    # "sign1":[B
    .end local v5    # "source":[B
    .end local v6    # "source1":[B
    .end local v7    # "v":[Ljava/lang/Object;
    .end local v8    # "yes":Z
    :cond_58
    :goto_58
    return-void

    .line 197
    :catch_59
    move-exception v0

    .line 198
    .local v0, "e":Ljava/lang/Exception;
    invoke-virtual {v0}, Ljava/lang/Exception;->printStackTrace()V

    goto :goto_58
.end method

.method public static sign([B[B)[B
    .registers 8
    .param p0, "keyInByte"    # [B
    .param p1, "source"    # [B

    .prologue
    .line 40
    :try_start_0
    new-instance v3, Ljava/security/spec/PKCS8EncodedKeySpec;

    invoke-direct {v3, p0}, Ljava/security/spec/PKCS8EncodedKeySpec;-><init>([B)V

    .line 41
    .local v3, "priv_spec":Ljava/security/spec/PKCS8EncodedKeySpec;
    const-string v5, "RSA"

    invoke-static {v5}, Ljava/security/KeyFactory;->getInstance(Ljava/lang/String;)Ljava/security/KeyFactory;

    move-result-object v1

    .line 42
    .local v1, "mykeyFactory":Ljava/security/KeyFactory;
    invoke-virtual {v1, v3}, Ljava/security/KeyFactory;->generatePrivate(Ljava/security/spec/KeySpec;)Ljava/security/PrivateKey;

    move-result-object v2

    .line 43
    .local v2, "privKey":Ljava/security/PrivateKey;
    const-string v5, "SHA1withRSA"

    invoke-static {v5}, Ljava/security/Signature;->getInstance(Ljava/lang/String;)Ljava/security/Signature;

    move-result-object v4

    .line 44
    .local v4, "sig":Ljava/security/Signature;
    invoke-virtual {v4, v2}, Ljava/security/Signature;->initSign(Ljava/security/PrivateKey;)V

    .line 45
    invoke-virtual {v4, p1}, Ljava/security/Signature;->update([B)V

    .line 46
    invoke-virtual {v4}, Ljava/security/Signature;->sign()[B
    :try_end_1e
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_1e} :catch_20

    move-result-object v5

    .line 48
    .end local v1    # "mykeyFactory":Ljava/security/KeyFactory;
    .end local v2    # "privKey":Ljava/security/PrivateKey;
    .end local v3    # "priv_spec":Ljava/security/spec/PKCS8EncodedKeySpec;
    .end local v4    # "sig":Ljava/security/Signature;
    :goto_1f
    return-object v5

    .line 47
    :catch_20
    move-exception v0

    .line 48
    .local v0, "e":Ljava/lang/Exception;
    const/4 v5, 0x0

    goto :goto_1f
.end method

.method public static verify([B[B[B)Z
    .registers 9
    .param p0, "keyInByte"    # [B
    .param p1, "source"    # [B
    .param p2, "sign"    # [B

    .prologue
    .line 65
    :try_start_0
    const-string v5, "RSA"

    invoke-static {v5}, Ljava/security/KeyFactory;->getInstance(Ljava/lang/String;)Ljava/security/KeyFactory;

    move-result-object v1

    .line 66
    .local v1, "mykeyFactory":Ljava/security/KeyFactory;
    const-string v5, "SHA1withRSA"

    invoke-static {v5}, Ljava/security/Signature;->getInstance(Ljava/lang/String;)Ljava/security/Signature;

    move-result-object v4

    .line 67
    .local v4, "sig":Ljava/security/Signature;
    new-instance v3, Ljava/security/spec/X509EncodedKeySpec;

    invoke-direct {v3, p0}, Ljava/security/spec/X509EncodedKeySpec;-><init>([B)V

    .line 68
    .local v3, "pub_spec":Ljava/security/spec/X509EncodedKeySpec;
    invoke-virtual {v1, v3}, Ljava/security/KeyFactory;->generatePublic(Ljava/security/spec/KeySpec;)Ljava/security/PublicKey;

    move-result-object v2

    .line 69
    .local v2, "pubKey":Ljava/security/PublicKey;
    invoke-virtual {v4, v2}, Ljava/security/Signature;->initVerify(Ljava/security/PublicKey;)V

    .line 70
    invoke-virtual {v4, p1}, Ljava/security/Signature;->update([B)V

    .line 71
    invoke-virtual {v4, p2}, Ljava/security/Signature;->verify([B)Z
    :try_end_1e
    .catch Ljava/lang/Exception; {:try_start_0 .. :try_end_1e} :catch_20

    move-result v5

    .line 73
    .end local v1    # "mykeyFactory":Ljava/security/KeyFactory;
    .end local v2    # "pubKey":Ljava/security/PublicKey;
    .end local v3    # "pub_spec":Ljava/security/spec/X509EncodedKeySpec;
    .end local v4    # "sig":Ljava/security/Signature;
    :goto_1f
    return v5

    .line 72
    :catch_20
    move-exception v0

    .line 73
    .local v0, "e":Ljava/lang/Exception;
    const/4 v5, 0x0

    goto :goto_1f
.end method
